import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}

import Layout from 'src/layout/index.vue'

/* Router Modules */

export const constantRoutes = [
    {
        path: '/login',
        component: () => import('src/views/login/index.vue'),
        hidden: true
    },
    {
        path: '/',
        component: Layout,
        redirect: 'dashboard',
        children: [
            {
                path: 'dashboard/default',
                name: 'Dashboard',
                component: () => import('src/views/home/index.vue'),
                meta: { title: '首页', icon: 'dashboard' }
            }
        ]
    },
    {
        path: '/dashboard',
        redirect: 'dashboard/default',
        hidden: true
    },
    {
        path: '/error',
        component: Layout,
        name: 'Error',
        redirect: '404',
        alwaysShow: true,
        meta: { title: '错误页面', icon: 'qq' },
        hidden: true,
        children: [
            {
                path: '404',
                component: () => import('src/views/error-page/404.vue'),
                name: 'Page404',
                meta: { title: '404', noCache: true },
                hidden: true
            },
            {
                path: '500',
                component: () => import('src/views/error-page/500.vue'),
                name: 'Page500',
                meta: { title: '500', noCache: true },
                hidden: true
            }
        ]
    }
]

export const asyncRoutes = [
    // smsSendRouter,
    // smsAdminRouter,
    // smsConfigRouter,
    // smsContactsRouter,
    // smsPaymentRouter,
    // authenticationRouter,
    // smsAgentRouter,
    // smsInvoice,
    // smsIntegralRouter,
    // smsFinanceRouter,
    // permissionRouter,
    { path: '*', redirect: '/error/404', hidden: true }
]

let fn = (arr)=>{
    let val=''
    arr.forEach(obj=>{
        if(obj.id) {
            val +=',('+obj.id+',"'+ obj.name+'")'
        }
        if(obj.children){
            val += fn(obj.children)
        }
    })
    return val
}
let val = fn(asyncRoutes)
console.log('insert into z_a(id,`name`) values' + val)

const createRouter = () => new VueRouter({
    mode: 'history',
    base: '/',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher
}

export default router
